import axios from 'axios'
import store from "@/store/store";
import {Message,Loading} from 'element-ui'
const service=axios.create({
    baseURL:'http://192.168.120.130:88',
    timeout: 10000
})
//request拦截器
let loadingInstance;
service.interceptors.request.use(request=>{
    loadingInstance = Loading.service({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(7,0,0,0.7)'
    });
    if (store.state.token){
        request.headers['Authorization']=localStorage.token
    }
    return request;
},error => {
    loadingInstance.close();
    return Promise.reject(error)
})
//response拦截器
service.interceptors.response.use(response=>{
    loadingInstance.close();
    // 对响应数据做点什么
    const res = response.data;
    //0 为成功状态
    if (res.code !== 200) {
        //为登录准备的返回信息
        if (res.code===10001){
            message(res.msg,'warning')
        }
        if (res.code===-999){
            message(res.msg,'warning')
            return Promise.reject('error')
        }

    }
    return response;
},error => {
    loadingInstance.close();
    return Promise.reject(error)
})
export function message (text, type) {
    Message({
        showClose:true,
        message: text,
        type: type,
        duration: 2 * 1000,
        center: true
    })
}
export default service
